﻿/*
  Project:       DataAnalyse
  File:          ShopHandler.ashx
  Description:   
 *               The global verialbles are all in this file.
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Add log function.


Notes:

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Data;

using DataAnalyseTool.BusinessTier.Bll;

namespace DataAnalyseTool.BusinessTier.Handlers
{
  /// <summary>
  /// Summary description for ShopHandler
  /// </summary>
  public class ShopHandler : IHttpHandler
  {
    protected static readonly log4net.ILog log =
   log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    ShopBLL shopBll = new ShopBLL();
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string action = context.Request.Params["action"];
      string shopname = context.Request.Params["shopname"];
      string color = context.Request.Params["color"];

      //Delete
      string shopid = context.Request.Params["shopid"];

      Model.Shops shop = new Model.Shops();
      shop.ShopName = shopname;
      shop.ShopColor = "#" + color;

      try
      {
        switch (action)
        {
          case "get":
            GetAllShops(context);
            break;
          case "add":
            AddShop(shop, context);
            break;
          case "update":
            UpdateShop(shop, context);
            break;
          case "delete":
            DeleteShop(shopid, context);
            break;
        }
      }
      catch (Exception ex)
      {
        log.Error("操作店铺的时候发生错误，错误信息是： " + ex.Message);
        context.Response.Write(GlobalVar.ErrorFlag);
      }
    }

    private void UpdateShop(Model.Shops myShop, HttpContext context)
    {
      if (!shopBll.isExistShopName(myShop.ShopName))
      {
        context.Response.Write(GlobalVar.NoExistFlag);
        return;
      }
      shopBll.UpdateShop(myShop);
      context.Response.Write(GlobalVar.SuccessFlag);
      log.Info("成功更新了店铺[" + myShop.ShopName + "]的颜色");
    }

    //Delete Shop By ShopID
    private void DeleteShop(string shopid, HttpContext context)
    {
      if (shopid != "")
      {
        shopBll.DeleteShopByID(Convert.ToInt32(shopid));
        context.Response.Write(GlobalVar.SuccessFlag);
      }
      else
      {
        log.Warn("Shopid的值为空。");
        context.Response.Write(GlobalVar.ErrorFlag);
      }
    }

    //Get all shops...
    private void GetAllShops(HttpContext context)
    {

      IList<Shop> shops = new List<Shop>();


      DataTable dt = shopBll.GetAllShops();
      shops.Add(new Shop() { ShopID = "100000", ShopName = "选择店铺", ShopColor = "#000000" });

      for (int i = 0; i < dt.Rows.Count; i++)
      {
        Shop p = new Shop();
        p.ShopID = dt.Rows[i]["ShopID"].ToString();
        p.ShopName = dt.Rows[i]["ShopName"].ToString();
        p.ShopColor = dt.Rows[i]["ShopColor"].ToString();
        shops.Add(p);
      }

      JsonSerializerSettings setting = new JsonSerializerSettings();
      setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
      string result = JsonConvert.SerializeObject(shops, Formatting.Indented, setting);
      context.Response.Write(result);
    }

    //Add shop
    private void AddShop(Model.Shops myShop, HttpContext context)
    {

      if (!shopBll.isExistShopName(myShop.ShopName))
      {
        shopBll.AddShop(myShop);
        log.Info("添加店铺名[" + myShop.ShopName + "]成功");
        context.Response.Write(GlobalVar.SuccessFlag);
      }
      else
      {
        context.Response.Write(GlobalVar.RepeatFlag);
      }

    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }

  public class Shop
  {
    public string ShopID { get; set; }
    public string ShopName { get; set; }
    public string ShopColor { get; set; }
  }
}